package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.Category;
import com.jiawa.wiki.domain.CategoryExample;
import com.jiawa.wiki.mapper.CategoryMapper;
import com.jiawa.wiki.req.CategoryQueryReq;
import com.jiawa.wiki.req.CategorySaveReq;
import com.jiawa.wiki.response.CategoryResp;
import com.jiawa.wiki.response.PageResp;
import com.jiawa.wiki.utils.CopyUtil;
import com.jiawa.wiki.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    private final static Logger LOG = LoggerFactory.getLogger(CategoryService.class);

    /**
     * 支持分页查询：注意 PageHelper 只对一条查询sql有效，如果配置下有多个sql,应该写在要查询的sql上即可
     * @param req
     * @return
     */
    public PageResp<CategoryResp> list(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        LOG.info("总页数：{}", pageInfo.getPages());
        LOG.info("总行数：{}", pageInfo.getTotal());
        // 持久层的需要传化为 List<CategoryResp>
        /* List<CategoryResp> respList = new ArrayList<>();
         for (Category category : categoryList) {
            CategoryResp categoryResp = new CategoryResp();
            //BeanUtils.copyProperties(category, categoryResp);
            // 单体复制
            CategoryResp copy = CopyUtil.copy(category, CategoryResp.getClass());
            respList.add(copy);
        } */

        // 列表复制 上述代码简化
        List<CategoryResp> respList = CopyUtil.copyList(categoryList, CategoryResp.class);

        PageResp<CategoryResp> pageResp = new PageResp<>();
        pageResp.setList(respList);
        pageResp.setTotal(pageInfo.getTotal());
        LOG.info("{}", respList);
        return pageResp;
    }

    /**
     * 查询所有分类
     * @return
     */
    public List<CategoryResp> all() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        // 列表复制
        List<CategoryResp> respList = CopyUtil.copyList(categoryList, CategoryResp.class);
        LOG.info("{}", respList);
        return respList;
    }

    /**
     * 保存分类（看请求参数id是否为空） 修改分类
     * @param req
     */
    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        if (ObjectUtils.isEmpty(category.getId())) {
            // 新增 设置id 此ID是雪花算法实现 不是uuid 和自增ID
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        } else {
            // 修改
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
